package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"zhihu-new/models/mysql"

	"zhihu-new/zhihurpc/internal/svc"
	"zhihu-new/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PrefaceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPrefaceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PrefaceLogic {
	return &PrefaceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PrefaceLogic) Preface(in *zhihurpc.PrefaceRequest) (*zhihurpc.PrefaceResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Model:    gorm.Model{ID: uint(in.UserId)},
		Images:   in.Images,
		Nickname: in.Nickname,
		Info:     in.Info,
		Address:  in.Address,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("完善失败")
	}

	return &zhihurpc.PrefaceResponse{UserId: int64(user.ID)}, nil
}
